import routes from '@/routes';
import { Breadcrumb } from 'antd';
import { memo } from 'react';
import { useLocation } from 'umi';

function getBreadcrumbs(routes: any[], pathnames: string, breadcrumbs: any[]) {
  for (const route of routes) {
    const { name } = route;

    if (route.path === pathnames) {
      breadcrumbs.push({
        title: name,
      });
    } else if (pathnames.includes(route.path) && route.routes) {
      breadcrumbs.push({
        title: name,
      });
      getBreadcrumbs(route.routes, pathnames, breadcrumbs);
    }
  }
  return breadcrumbs;
}

const BreadCrumb: React.FC = () => {
  const location = useLocation();
  const breadcrumbs = getBreadcrumbs(routes, location.pathname, []);
  return (
    <>
      <Breadcrumb items={breadcrumbs} />
    </>
  );
};

export default memo(BreadCrumb);
